<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员欢迎语
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
			array('op'=>'list','title'=>'欢迎语列表'),
			array('op'=>'edit','title'=>'添加欢迎语'),
);
$return['types']=array(
	'0'=>'文本',
	'1'=>'图片',
	'2'=>'语音（音乐）',
	'3'=>'视频',
	'4'=>'图文',
);
//会员欢迎语
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_welcome',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'groupid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['qunnum']=0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'欢迎语使用说明：https://work.weixin.qq.com/api/doc/90000/90135/92137
		</br>1、欢迎语又称新好友自动回复，此处可添加文字、图片、图文链接及小程序，客户来了不用担心冷场！
		</br>2、每个企业成员均可以拥有不同的欢迎语。当通用的欢迎语和个人专属的欢迎语并存的情况下，优先自动回复个人专属的欢迎语。
		</br>3、如果企业在企业微信后台为相关成员配置了可用的欢迎语，使用第三方系统配置欢迎语，则均不起效，推送的还是企业微信官方的。
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'type','title'=>'欢迎语类型','type'=>'radio','options'=>'types'),
			//array('name'=>'content','title'=>'欢迎语内容','type'=>'string'),
			array('name'=>'bumenid','title'=>'所属部门','type'=>'select','options'=>'groups','option_value'=>'bumenid'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'is_default','title'=>'默认','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'type','width'=>15,'title'=>'欢迎语类型','type'=>'radio','options'=>'types'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['htmls']=$htmls;
	$return['groups']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_welcome',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('type'=>0);
	}
	$item['image']=$item['image']?json_decode($item['image'],true):array('title'=>'');
	$item['link']=$item['link']?json_decode($item['link'],true):array('title'=>'');
	$item['miniprogram']=$item['miniprogram']?json_decode($item['miniprogram'],true):array('title'=>'');
	$item['video']=$item['video']?json_decode($item['video'],true):array('title'=>'');
	$item['file']=$item['file']?json_decode($item['file'],true):array('title'=>'');
	$return['groups']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'));
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写欢迎语名称',true);
	}
	if(!$postdata['content'] && !$postdata['type']){
		exi('请填写欢迎语内容',true);
	}
	if(!$postdata['medias'] && $postdata['type']){
		exi('请填写欢迎语内容',true);
	}
	if($postdata['miniprogram']['image']){
		$postdata['miniprogram']['pic_media_id']=model('core/wxwork_userapi')->upload_media($postdata['miniprogram']['image'],'image');
	}
	if($postdata['video']['url']){
		$postdata['video']['media_id']=model('core/wxwork_userapi')->upload_media($postdata['video']['url'],'video');
	}
	if($postdata['image']['pic_url']){
		$postdata['image']['media_id']=model('core/wxwork_userapi')->upload_media($postdata['image']['pic_url'],'image');
	}
	$postdata['content']=str_replace('\"','"',$postdata['content']);
	$postdata['content']=str_replace('"','\"',$postdata['content']);
	$data=array(
		'title'=>trim($postdata['title']),
		'type'=>intval($postdata['type']),
		'bumenid'=>intval($postdata['bumenid']),
		'content'=>$postdata['content'],
		'miniprogram'=>ijson_encode($postdata['miniprogram']),
		'image'=>ijson_encode($postdata['image']),
		'video'=>ijson_encode($postdata['video']),
		'link'=>ijson_encode($postdata['link']),
		'status'=>1,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_welcome',$data,array('merchid'=>$_SESSION['onemerchid'],'id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_welcome',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户组！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_welcome',array('status'=>intval($post['status'])),array('merchid'=>$_SESSION['onemerchid'],'id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['is_default'])){
		if($post['is_default']){
			pdo_update('jy_scrm_welcome',array('is_default'=>0),array('merchid'=>$_SESSION['onemerchid'],'is_default'=>1,'uniacid'=>$_SESSION['uniacid']));
		}
		pdo_update('jy_scrm_welcome',array('is_default'=>intval($post['is_default'])),array('merchid'=>$_SESSION['onemerchid'],'id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_welcome',array('merchid'=>$_SESSION['onemerchid'],'id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}